package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.DeptZhuanjia;
import com.ruoyi.system.domain.JingfeiDetail;
import com.ruoyi.system.service.IDeptZhuanjiaService;
import com.ruoyi.system.service.IDeptidOlddeptidService;
import com.ruoyi.system.service.IJingfeiDetailService;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 专家经费Controller
 *
 * @author ruoyi
 * @date 2023-02-16
 */
@Controller
@RequestMapping("/system/jingfeidetail")
public class JingfeiDetailController extends BaseController
{
    private String prefix = "system/jingfeidetail";

    @Autowired
    private IJingfeiDetailService jingfeiDetailService;
    @Autowired
    private IDeptZhuanjiaService deptZhuanjiaService;
    @Autowired
    private IDeptidOlddeptidService deptidOlddeptidService;

    @RequiresPermissions("system:jingfeidetail:view")
    @GetMapping()
    public String jingfeidetail()
    {
        return prefix + "/jingfeidetail";
    }

    /**
     * 查询专家经费列表
     */
    @RequiresPermissions("system:jingfeidetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JingfeiDetail jingfeiDetail)
    {
        startPage();
        List<JingfeiDetail> list = jingfeiDetailService.selectJingfeiDetailList(jingfeiDetail);
        return getDataTable(list);
    }

    /**
     * 导出专家经费列表
     */
    @RequiresPermissions("system:jingfeidetail:export")
    @Log(title = "专家经费", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JingfeiDetail jingfeiDetail)
    {
        List<JingfeiDetail> list = jingfeiDetailService.selectJingfeiDetailList(jingfeiDetail);
        ExcelUtil<JingfeiDetail> util = new ExcelUtil<JingfeiDetail>(JingfeiDetail.class);
        return util.exportExcel(list, "专家经费数据");
    }

    /**
     * 新增专家经费
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存专家经费
     */
    @RequiresPermissions("system:jingfeidetail:add")
    @Log(title = "专家经费", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JingfeiDetail jingfeiDetail)
    {
        return toAjax(jingfeiDetailService.insertJingfeiDetail(jingfeiDetail));
    }

    /**
     * 修改专家经费
     */
    @RequiresPermissions("system:jingfeidetail:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        JingfeiDetail jingfeiDetail = jingfeiDetailService.selectJingfeiDetailById(id);
        mmap.put("jingfeiDetail", jingfeiDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存专家经费
     */
    @RequiresPermissions("system:jingfeidetail:edit")
    @Log(title = "专家经费", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JingfeiDetail jingfeiDetail)
    {
        return toAjax(jingfeiDetailService.updateJingfeiDetail(jingfeiDetail));
    }

    /**
     * 删除专家经费
     */
    @RequiresPermissions("system:jingfeidetail:remove")
    @Log(title = "专家经费", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jingfeiDetailService.deleteJingfeiDetailByIds(ids));
    }

    @PostMapping("jingfeilishi")
    @ResponseBody
    public JsonResult<?> jingfeilishi(@RequestParam("zhuanjiaid")String ppZhuanjiaId){

        DeptZhuanjia tempDeptZhuanjia = new DeptZhuanjia();
        tempDeptZhuanjia.setZhuanjiaid(ppZhuanjiaId);
        tempDeptZhuanjia.setIsvalid((long)1);

        List<DeptZhuanjia> tempDeptZhuanjiaList = deptZhuanjiaService.selectDeptZhuanjiaList(tempDeptZhuanjia);
        if (tempDeptZhuanjiaList.size()!=1){
            return JsonResult.getErrorResult("该专家查询失败");
        }else {
            DeptZhuanjia mmDeptZhuanjia = tempDeptZhuanjiaList.get(0);
            String mmDeptId = deptidOlddeptidService.OldDeptIdToDeptId(mmDeptZhuanjia.getDeptid());

            JingfeiDetail tempJingfeiDetail =  new JingfeiDetail();
            tempJingfeiDetail.setZhuangjiaid(ppZhuanjiaId);
            tempJingfeiDetail.setDeptid(mmDeptId);

            List<JingfeiDetail> mmList = jingfeiDetailService.selectJingfeiDetailList(tempJingfeiDetail);

            return JsonResult.getSuccessResult(mmList);
        }

    }
}
